package com.bong.panpan.bongcharttools.custom;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by panpan on 2017/11/6.
 */

public class UserInfoRecord implements Serializable {
    public ArrayList<String> editText = new ArrayList<>();
    public ArrayList<Boolean> radioChecks = new ArrayList<>();
    public ArrayList<Boolean> boxChecks = new ArrayList<>();
    /**
     * 参考数据:
     * instrumentType血压计类型（欧姆龙：0 水银：1）
     * highBloodPressure高压
     * lowBloodPressure低压
     */
    public byte instrumentType, highBloodPressure, lowBloodPressure;

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("编码,序列号,身高(cm),体重(kg),年龄,性别\n");
        sb.append(editText.get(0) +
                "," + editText.get(1) +
                "," + editText.get(2) +
                "," + editText.get(3) +
                "," + editText.get(4) +
                "," + (radioChecks.get(0) ? "男" : "女"));
        sb.append("\n");
        sb.append("\n1. 是否吃过降压药？," + (radioChecks.get(2) ? "是" : "否"));
        sb.append("\n2. 是否服用血管舒张药？," + (radioChecks.get(4) ? "是" : "否"));
        sb.append("\n3. 30分钟内喝过咖啡？," + (radioChecks.get(6) ? "是" : "否"));
        sb.append("\n4. 30分钟内吸过烟？," + (radioChecks.get(8) ? "是" : "否"));
        sb.append("\n5. 每天锻炼多少分钟？,"
                + (radioChecks.get(10) ? "<15" : "")
                + (radioChecks.get(11) ? "15-30" : "")
                + (radioChecks.get(12) ? "30-60" : "")
                + (radioChecks.get(13) ? ">60" : "")
        );
        sb.append("\n6. 家族病史");
        if (boxChecks.get(0))
            sb.append(",心血管病");
        if (boxChecks.get(1))
            sb.append(",高血压");
        if (boxChecks.get(2))
            sb.append(",糖尿病");

        sb.append("\n7. 个人喜好");
        if (boxChecks.get(3))
            sb.append(",喝酒");
        if (boxChecks.get(4))
            sb.append(",喝咖啡");
        if (boxChecks.get(5))
            sb.append(",吸烟");

        sb.append("\n8. 慢性病");
        if (boxChecks.get(6))
            sb.append(",糖尿病");
        if (boxChecks.get(7))
            sb.append(",帕金森氏症");
        if (boxChecks.get(8))
            sb.append(",心脏病");
        if (boxChecks.get(9))
            sb.append(",高血压");
        if (boxChecks.get(10))
            sb.append(",高胆固醇");
        if (boxChecks.get(11))
            sb.append(",高血糖");
        sb.append("\n");
        sb.append("\n胆固醇,血糖\n");
        sb.append(editText.get(5) + "," + editText.get(6));
        sb.append("\n");
        sb.append("\n参考数据");
        sb.append("\n血压计,高压,低压\n");
        sb.append((instrumentType == 0 ? "欧姆龙" : "水银") +
                "," + MyUtil.byte2Int(highBloodPressure)
                + "," + MyUtil.byte2Int(lowBloodPressure));
        return sb.toString();
    }

    public static boolean save(UserInfoRecord record, String path) {
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(path));
            outputStream.writeObject(record);
            outputStream.close();
            return true;
        } catch (Exception e) {
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (Exception e1) {

            }
        }
        return false;
    }

    public static UserInfoRecord load(String path) {
        UserInfoRecord record = null;
        ObjectInputStream inputStream = null;
        try {

            inputStream = new ObjectInputStream(new FileInputStream(path));
            record = (UserInfoRecord) inputStream.readObject();
            inputStream.close();
        } catch (Exception e) {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (Exception e1) {

            }
        }
        return record;
    }
}
